﻿using Microsoft.Data.ConnectionUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeBuilder
{
    public partial class Form1 : Form
    {
        private DatabaseService service;
        string lastFileFolder = "";
        public Form1()
        {
            service = new DatabaseService();
            InitializeComponent();
            textConnectionString.DataBindings.Add("Text", service, "ConnectionString", false, DataSourceUpdateMode.OnPropertyChanged);
            textNameSpace.DataBindings.Add("Text", service, "NameSpace", false, DataSourceUpdateMode.OnPropertyChanged);
        }

        private void buttonChooseDbServer_Click(object sender, EventArgs e)
        {
            var dialog = new DataConnectionDialog();
            DataSource.AddStandardDataSources(dialog);
            var source = new DataSource("MySQL", "MySQL Server");
            source.Providers.Add(new DataProvider("MySql.Data.MySqlClient", "MySQL Data Provider", "MySQL", "MySQL Data Provider Description", typeof(MySql.Data.MySqlClient.MySqlConnection),typeof(MySQLConnectionUIControl), typeof(MySqlConnectionProperties)));
            dialog.DataSources.Add(source);
            dialog.DataSources.Add(DataSource.SqlDataSource);
            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
            if (DataConnectionDialog.Show(dialog) == DialogResult.OK)
            {
                this.textConnectionString.Text = dialog.ConnectionString;
                this.service.Provider = dialog.SelectedDataProvider.Name;
            }
        }

        private void buttonLoadDataSource_Click(object sender, EventArgs e)
        {
            service.Load();
            listTables.DataSource = service.Source;
            listTables.DisplayMember = "Entity";
            grpTable.Enabled = true;
        }

        private void listTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            var table = listTables.SelectedItem as Table;
            if (table != null)
            {
                this.gridColumns.DataSource = table.Columns;
            }
        }

        private void btnClass_Click(object sender, EventArgs e)
        {

            var table = listTables.SelectedItem as Table;
            if (table != null)
            {
                new FormResult(service.BuildClass(table), "", table.Entity).ShowDialog();
            }
        }

        private void btnBatchClass_Click(object sender, EventArgs e)
        {
            //获取指定路径
            if (!string.IsNullOrEmpty(lastFileFolder))
            {
                folderBrowserDialog.SelectedPath = lastFileFolder;
            }
            folderBrowserDialog.Description = "请选择要导出的路径";
            folderBrowserDialog.ShowDialog();
            if (!string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
            {
                for (int i = 0; i < listTables.Items.Count; i++)
                {
                    var table = listTables.Items[i] as Table;
                    System.IO.File.WriteAllText(Path.Combine(folderBrowserDialog.SelectedPath, table.Entity + ".cs"),
                        service.BuildClass(table)
                        );
                }
                MessageBox.Show("OK");
            }
        }

    }
}
